<template>
  <div class="device-material-selector">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="search-form">
      <el-form-item>
        <el-input
          v-model="queryParams.materialName"
          placeholder="输入物料名称搜索"
          clearable
          @keyup.enter="handleQuery"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="queryParams.materialCode"
          placeholder="输入物料编码搜索"
          clearable
          @keyup.enter="handleQuery"
        >
          <template #prefix>
            <el-icon><Document /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery" class="btn-with-icon">
          <el-icon><Search /></el-icon>搜索
        </el-button>
        <el-button @click="resetQuery" class="btn-with-icon">
          <el-icon><Refresh /></el-icon>重置
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 设备物料表格 -->
    <el-table
      v-loading="loading"
      :data="deviceMaterialList"
      border
      stripe
      height="400px"
      highlight-current-row
      @row-click="handleRowClick"
    >
      <el-table-column type="index" width="60" label="序号" align="center" />
      <el-table-column prop="materialCode" label="物料编码" min-width="120" show-overflow-tooltip />
      <el-table-column prop="materialName" label="物料名称" min-width="150" show-overflow-tooltip />
      <el-table-column prop="specification" label="规格" min-width="120" show-overflow-tooltip />
      <el-table-column prop="materialType" label="材质" width="100" align="center" show-overflow-tooltip />
      <el-table-column prop="unit" label="单位" width="70" align="center" />
      <el-table-column prop="price" label="单价" width="100" align="right">
        <template #default="scope">
          <span class="price-value">{{ formatPrice(scope.row.price) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template #default="scope">
          <el-button type="primary" link @click.stop="selectMaterial(scope.row)" class="btn-action">
            <el-icon><Check /></el-icon>选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="queryParams.current"
      v-model:page-size="queryParams.size"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh, Document, Check } from '@element-plus/icons-vue'
import { getDeviceMaterialList } from '@/api/deviceMaterial'
import type { DeviceMaterial, DeviceMaterialQuery } from '@/api/deviceMaterial/type'

const emit = defineEmits(['update:model-value', 'change', 'select'])

// 表格数据加载
const loading = ref(false)
const deviceMaterialList = ref<DeviceMaterial[]>([])
const total = ref(0)

// 查询参数
const queryParams = reactive<DeviceMaterialQuery>({
  current: 1,
  size: 10,
  materialCode: '',
  materialName: '',
  materialType: ''
})

// 获取设备物料列表
const getList = async () => {
  loading.value = true
  try {
    const res = await getDeviceMaterialList(queryParams)
    if (res && res.data) {
      deviceMaterialList.value = res.data.records || []
      total.value = res.data.total || 0
    }
  } catch (error) {
    console.error('获取设备物料列表失败:', error)
    ElMessage.error('获取设备物料列表失败')
  } finally {
    loading.value = false
  }
}

// 查询操作
const handleQuery = () => {
  queryParams.current = 1
  getList()
}

// 重置查询
const resetQuery = () => {
  queryParams.materialCode = ''
  queryParams.materialName = ''
  queryParams.materialType = ''
  queryParams.current = 1
  getList()
}

// 选择物料
const selectMaterial = (row: DeviceMaterial) => {
  emit('update:model-value', row)
  emit('change', row)
  emit('select', row)
}

// 行点击事件
const handleRowClick = (row: DeviceMaterial) => {
  selectMaterial(row)
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  queryParams.size = size
  getList()
}

// 当前页变化
const handleCurrentChange = (current: number) => {
  queryParams.current = current
  getList()
}

// 格式化价格
const formatPrice = (price: string | undefined) => {
  if (!price) return '¥0.00'
  return `¥${Number(price).toFixed(2)}`
}

// 初始化
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
.device-material-selector {
  padding: 15px;
  
  .search-form {
    margin-bottom: 15px;
    display: flex;
    flex-wrap: wrap;
    
    .el-form-item {
      margin-bottom: 15px;
      margin-right: 15px;
    }
  }
  
  .price-value {
    font-weight: 600;
    color: #f56c6c;
  }
  
  .pagination {
    margin-top: 15px;
    justify-content: flex-end;
  }
  
  .btn-with-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    
    .el-icon {
      margin-right: 4px;
    }
  }
  
  .btn-action {
    padding: 4px 8px;
    
    &:hover {
      background-color: rgba(64, 158, 255, 0.1);
      border-radius: 4px;
    }
    
    .el-icon {
      margin-right: 4px;
    }
  }
}
</style> 